package test;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.*;
import java.awt.event.InputEvent;
import java.io.File;
import java.util.List;

public class FileDropDemo extends JFrame {

    public FileDropDemo() {
        setTitle("文件拖拽演示");
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 使用 JPanel 替代 JLabel 作为拖放容器
        JPanel dropPanel = new JPanel(new GridBagLayout());
        dropPanel.setBackground(Color.WHITE);
        
        JLabel hintLabel = new JLabel("拖拽文件到此区域", SwingConstants.CENTER);
        hintLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        dropPanel.add(hintLabel);

        add(dropPanel, BorderLayout.CENTER);

        // 关键修复：延迟初始化 DropTarget
        SwingUtilities.invokeLater(() -> {
            DropTarget dropTarget = new DropTarget(dropPanel, new DropTargetAdapter() {
                @Override
                public void dragEnter(DropTargetDragEvent dtde) {
                    System.out.println("[事件触发] dragEnter");
                    if (isDragAcceptable(dtde)) {
                        dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
                        dropPanel.setBackground(new Color(200, 255, 200));
                    }
                }

                @Override
                public void dragOver(DropTargetDragEvent dtde) {
                    if (isDragAcceptable(dtde)) {
                        dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
                    }
                }

                @Override
                public void dragExit(DropTargetEvent dte) {
                    dropPanel.setBackground(Color.WHITE);
                }

                @Override
                public void drop(DropTargetDropEvent dtde) {
                    try {
                        System.out.println("[事件触发] drop");
                        if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                            
                            List<File> files = (List<File>) dtde.getTransferable()
                                    .getTransferData(DataFlavor.javaFileListFlavor);
                            
                            for (File file : files) {
                                System.out.println("接收到文件: " + file.getAbsolutePath());
                            }
                            
                            dtde.dropComplete(true);
                            dropPanel.setBackground(Color.WHITE);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        dtde.dropComplete(false);
                    }
                }

                private boolean isDragAcceptable(DropTargetDragEvent event) {
                    return event.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
                }
            });
        });
    }

    public static void main(String[] args) {
    	Toolkit.getDefaultToolkit().addAWTEventListener(e -> {
            if (e instanceof InputEvent) {
                // 强制激活拖放功能
                ((Component)e.getSource()).enableInputMethods(true);
            }
        }, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
        // 修复1：禁用冲突的DnD系统属性
        System.setProperty("sun.awt.dnd.disabledDnd", "false");
        
        // 修复2：强制开启拖放调试模式
        System.setProperty("sun.awt.dnd.drag.debug", "true");
        System.setProperty("sun.awt.dnd.drop.debug", "true");

        SwingUtilities.invokeLater(() -> {
            new FileDropDemo().setVisible(true);
            System.out.println("程序已启动，等待文件拖入...");
        });
    }
}